<?php

class ClubHistory extends Model {
  private $clubHistory;

  public function __construct() {
    parent::Model();
    $this->load->helper('date');
    $clubHistory = array("name"=>"",
        "arena"=>array(),
        "nation"=>"",
        "league"=>"",
        "seniors"=>array(),
        "youths"=>array(),
        "littleLeague"=>array(),
        "unreadNews"=>0,
        "unreadMessages"=>0,
        "latestDraft"=>array());
  }

  public function loadHistoryOf($clubId) {
    $this->db->where('id',$clubId);
    $query = $this->db->get('club');

    if (!$query || $query->num_rows() != 1) {
      log_message('error','Club not found in the database!');
    } else {
      $result = $query->row();
      $this->clubHistory['name'] = $result->name;

      $this->clubHistory['arena'] = $this->loadHistory("arena",$result->arenaId);

      $this->clubHistory['nation'] = $result->nation;
      $this->clubHistory['league'] = $result->league;

      $this->clubHistory['seniors'] = $this->loadHistory('team',$result->seniorSquadId);

      $this->clubHistory['seniors'] = $this->loadHistory('team',$result->youthSquadId);

      $this->clubHistory['littleLeague'] = $this->loadHistory('littleleague', $result->littleLeagueId );

      $this->clubHistory['latestDraft'] = $this->loadHistory('latestdraft',$result->latestDraftId);
    }
  }

  private function loadHistory($table,$lastId) {
    $tmpHistory = array();
    $tmpId = $lastId;
    $tmpDate = time();

    while (true) {
      $this->db->where('id',$tmpId);
      $query = $this->db->get($table);

      if (!$query || $query->num_rows() != 1) {
        log_message('error','Error during history');
      } else {
        $result = $query->row_array();
        array_push($tmpHistory,$result);
        array_push($tmpHistory,date("U",$tmpDate));
      }

      $this->db->where('newId',$tmpId);
      $this->db->where('table',$table);
      $query = $this->db->get('clubhistory');

      if (!$query) {
        log_message('error', 'Database error!');
      }
      if ($query->num_rows()==0) {
        $query->free_result();
        break;
      } else {
        $result = $query->row_array();
        $tmpId = $result['oldId'];
        $tmpDate = mysql_to_unix($result['insertTime']);
      }
    };
    //$tmpHistory = purge($tmpHistory);
    return array_reverse($tmpHistory);
  }

  //  private function purge($history) {
  //
  //    return $purgedHistory;
  //  }

  public function getArenaHistory() {
    return $this->clubHistory['arena'];
  }

  public function getSeniorHistory() {
    return $this->clubHistory['seniors'];
  }

  public function getYouthHistory() {
    return $this->clubHistory['youths'];
  }

  public function getLittleLeagueHistory() {
    return $this->clubHistory['littleLeague'];
  }

  public function getLatestDraftHistory() {
    return $this->clubHistory['latestDraft'];
  }

  public function getHistoryOf($attribute,$history) {
    $dataArray = array();
    $dateArray = array();
    $tmpArray = array();
    $tmpHistory = $this->clubHistory[$history];

    for ($i=0;$i<count($tmpHistory);$i=$i+2) {
      array_push($dataArray,resolveTechName($tmpHistory[$i+1][$attribute]));
      array_push($dateArray,$tmpHistory[$i]);
    };
    array_push($tmpArray,$dataArray);
    array_push($tmpArray,$dateArray);

    return $tmpArray;
  }

  public function getLatestOf($attribute,$history) {
    $tmpArray = array();
    $tmpHistory = $this->clubHistory[$history];

    $i=count($tmpHistory);

    array_push($tmpArray,resolveTechName($tmpHistory[$i-1][$attribute]));
    array_push($tmpArray,$tmpHistory[$i-2]);

    return $tmpArray;
  }
}

?>